package com.ruoyi.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jl.domain.Game;
import com.ruoyi.jl.domain.Vip;
import com.ruoyi.jl.mapper.VipMapper;
import com.ruoyi.jl.service.IVipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * vip等级 服务层实现
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements IVipService {

    @Autowired
    VipMapper vipMapper;

    @Override
    public List<Vip> list(Vip vip){
        LambdaQueryWrapper<Vip> wrapper = new LambdaQueryWrapper<>();
        if(vip != null){
            if(StringUtils.isNotEmpty(vip.getId())){
                wrapper.eq(Vip::getId, vip.getId());
            }
            if(StringUtils.isNotEmpty(vip.getName())){
                wrapper.eq(Vip::getName, vip.getName());
            }
            if(StringUtils.isNotEmpty(vip.getGoldLimit())){
                wrapper.eq(Vip::getGoldLimit, vip.getGoldLimit());
            }
            if(StringUtils.isNotEmpty(vip.getContext())){
                wrapper.eq(Vip::getContext, vip.getContext());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Vip selectDefaultTree(Integer vipId) {
        Vip vip = null;
        if(vipId == 0) {// 查询第一条数据
            QueryWrapper<Vip> wrapper = new QueryWrapper<>();
            wrapper.last("limit 1");
            vip = vipMapper.selectOne(wrapper);
        } else {
            vip = vipMapper.selectById(vipId);
        }
        return vip;
    }

    @Override
    public List<Map<String, Object>> treeData() {
        List<Map<String, Object>> trees = new ArrayList<>();
        Map<String, Object> deptMap = new HashMap<>(5);
        deptMap.put("id", -1);
        deptMap.put("pId", 0);
        deptMap.put("name", "VIP选择");
        deptMap.put("title", "VIP选择");
        deptMap.put("checked", false);
        trees.add(deptMap);
        List<Vip> vips = vipMapper.selectList(null);
        for(Vip vip : vips) {
            deptMap = new HashMap<>(5);
            deptMap.put("id", vip.getId());
            deptMap.put("pId", -1);
            deptMap.put("name", vip.getName());
            deptMap.put("title", vip.getName());
            deptMap.put("checked", false);
            trees.add(deptMap);
        }
        return trees;
    }
}
